package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止请求中文乱码问题
        request.setCharacterEncoding("UTF-8");
        //username=admin&password=123
        //request.getParameter 根据参数name 获取其value
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        //设置响应的内容类型
        //解决响应中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        //向前端返回登录成功信息
        //获取响应对象字符输出流
        PrintWriter writer = response.getWriter();
        if ("admin".equals(username) && "123".equals(password)) {
            writer.write("<h1>恭喜" + username + ", 登陆成功！</h1>");
        } else {
            writer.write("<h1>恭喜登录失败</h1>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
